package com.kuhyygy.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.kuhyygy.R;
import com.kuhyygy.bean.User;

/**
 * 自定义的Adapter
 * 一、要构造方法；
 * 二、重写getView方法
 * 
 * 
 * @author acer
 *
 */
public class DbUserAdapter extends ArrayAdapter<User> {
	
	private int resource;

	/**
	 * 
	 * @param context	父的Activity
	 * @param resource	ListView的Layout
	 * @param objects
	 */
	public DbUserAdapter(Context context, int resource, List<User> objects) {
		super(context, resource, objects);
		this.resource = resource;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		User e = getItem(position);
		View view;
		ViewHolder viewHolder;
		if (null == convertView) {	// 布局不重复加载
			view = LayoutInflater.from(getContext()).inflate(resource, null);
			viewHolder = new ViewHolder();
			viewHolder.idTextViewHolder = (TextView) view.findViewById(R.id.dbUserIdText);
			viewHolder.nameTextViewHolder = (TextView) view.findViewById(R.id.dbUserNameText);
			viewHolder.genderTextViewHolder = (TextView) view.findViewById(R.id.dbUserGenderText);
			view.setTag(viewHolder);
		} else {
			view = convertView;
			viewHolder = (ViewHolder) view.getTag();
		}
		viewHolder.idTextViewHolder.setText(String.valueOf(e.getId()));
		viewHolder.nameTextViewHolder.setText(e.getName());
		viewHolder.genderTextViewHolder.setText(e.getGender());
		return view;
	}
	
	/**
	 * 控件不重复加载
	 * @author acer
	 *
	 */
	class ViewHolder {
		TextView idTextViewHolder;
		TextView nameTextViewHolder;
		TextView genderTextViewHolder;
		public TextView getNameTextViewHolder() {
			return nameTextViewHolder;
		}
		public void setNameTextViewHolder(TextView nameTextViewHolder) {
			this.nameTextViewHolder = nameTextViewHolder;
		}
		public TextView getGenderTextViewHolder() {
			return genderTextViewHolder;
		}
		public void setGenderTextViewHolder(TextView genderTextViewHolder) {
			this.genderTextViewHolder = genderTextViewHolder;
		}
		public TextView getIdTextViewHolder() {
			return idTextViewHolder;
		}
		public void setIdTextViewHolder(TextView idTextViewHolder) {
			this.idTextViewHolder = idTextViewHolder;
		}
		
	}

}
